package com.mapgis

import com.mapgis.constant.Constants
import com.mapgis.utils.{IOutil, JDBCUtil}

/**
  * 1. 检查市办物理删除的数据，并在本地进行删除
  * 2. 检查龙岗的数据是否有遗漏，对遗漏的数据写入文件中待同步工具进行同步
  */
object DataCheck {

  def main(args: Array[String]): Unit = {
    var lgTable = getTargetTableByParams(args(0))
    var lgIDTable = getIDTableByParams(args(0))

    val deleteIdList = getShibanDelete(lgTable,lgIDTable,args(0))
    deleteRecordsLG(lgTable,deleteIdList)

    val saveIdList = getWaitSync(lgIDTable,lgTable,args(0))
//    saveIdToFile(args(0),saveIdList)
    IOutil.writeIDListToFile(args(0),saveIdList,Constants.ORACLE_CURSOR)
    println(s"龙岗数据库中缺的数据已存入到文件中,保存的数量为: ${saveIdList.length} 条")

    //将相关表的字段更新至文件中
    saveFieldToField(args(0))

  }

  /**
    * 通过参数获得正式表的表名
    * @param params
    * @return
    */
  def getTargetTableByParams(params: String) ={
    var tableName=""
    params match {
      case "building" => tableName=Constants.TABLE_LG_BUILDING
      case "house" => tableName=Constants.TABLE_LG_HOUSE
      case "resident" => tableName=Constants.TABLE_LG_RESIDENT
      case "residenthmt" => tableName=Constants.TABLE_LG_RESIDENT
      case "residentoutside" => tableName=Constants.TABLE_LG_RESIDENT
      case "residentlogout" => tableName=Constants.TABLE_LG_RESIDENTLOGOUT
      case "enterprise" => tableName=Constants.TABLE_LG_ENTERPRISEINFO
      case "qharea" => tableName=Constants.TABLE_LG_QHAREA
      case "qhteam" => tableName=Constants.TABLE_LG_QHTEAM
//      case "event" => tableName=Constants.TABLE_LG_IMP_EVENT
      case "enterpriseinfomap" => tableName=Constants.TABLE_LG_ENTERPRISEINFOMAP
    }
    tableName
  }

  /**
    * 通过参数获得全量同步ID的表名
    * @param params
    * @return
    */
  def getIDTableByParams(params: String) ={
    var tableName = ""
    params match {
      case "building" => tableName=Constants.ID_LG_BUILDING
      case "house" => tableName=Constants.ID_LG_HOUSE
      case "resident" => tableName=Constants.ID_LG_RESIDENT
      case "residenthmt" => tableName=Constants.ID_LG_RESIDENTHMT
      case "residentoutside" => tableName=Constants.ID_LG_RESIDENTOUTSIDE
      case "residentlogout" => tableName=Constants.ID_LG_RESIDENTLOGOUT
      case "enterprise" => tableName=Constants.ID_LG_ENTERPRISEINFO
      case "qharea" => tableName=Constants.ID_LG_QHAREA
      case "qhteam" => tableName=Constants.ID_LG_QHTEAM
      case "event" => tableName=Constants.ID_LG_EVENT
      case "enterpriseinfomap" => tableName=Constants.ID_LG_ENTERPRISEINFOMAP
    }
    tableName
  }

  /**
    * 获取市办进行了物理删除的数据的id
    * @param thisTable
    * @param joinTable
    * @param jobNmae  由于人口的id在转换进正式库的过程中做了处理，所以此处需要特殊处理
    * @return
    */
  def getShibanDelete(thisTable: String,joinTable: String,jobNmae: String) = {
    var sql = ""
    jobNmae match {
      case "resident" => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"a.id='1'||b.id","b.id is null and substr(a.id,1,1)='1'")
      case "residenthmt" => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"a.id='2'||b.id","b.id is null and substr(a.id,1,1)='2'")
      case "residentoutside" => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"a.id='3'||b.id||b.id","b.id is null and substr(a.id,1,1)='3'")
      case _ => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"a.id=b.id","b.id is null")
    }

    val idList = JDBCUtil.handExecuteQuery(sql).map(_.get("id".toUpperCase))
    idList
  }

  /**
    * 获取全量同步时待同步的数据
    * @param thisTable
    * @param joinTable
    * @param jobNmae
    * @return
    */
  def getWaitSync(thisTable: String,joinTable: String,jobNmae: String) = {
    var sql = ""
    jobNmae match {
      case "resident" => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"'1'||a.id=b.id","b.id is null and substr(a.id,1,1)='1'")
      case "residenthmt" => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"'2'||a.id=b.id","b.id is null and substr(a.id,1,1)='2'")
      case "residentoutside" => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"'3'||a.id||a.id=b.id","b.id is null and substr(a.id,1,1)='3'")
      case _ => sql = JDBCUtil.leftJoin("a.id",thisTable,joinTable,"a.id=b.id","b.id is null")
    }

    val idList = JDBCUtil.handExecuteQuery(sql).map(_.get("id".toUpperCase))
    idList
  }

  /**
    * 删除在龙岗数据中存在但是市办系统中不存在的数据
    * @param tableName
    * @param idList
    */
  def deleteRecordsLG(tableName: String,idList: List[String])={
    val deleteRecordsNum = JDBCUtil.deleteById(idList,tableName)
    println(s"市办删除的数据数为： ${idList.size},龙岗数据库执行删除操作删除的数据为： ${deleteRecordsNum}")
  }

  /**
    * 删除在龙岗数据中存在但是市办系统中不存在的数据
    * @param fileName
    * @param idList
    */
//  def saveIdToFile(fileName: String,idList: List[String])={
//    IOutil.clearFile(fileName)
//    for (i <- 0 to idList.size/Constants.ORACLE_CURSOR){
//      var newIdList = idList.slice(i*Constants.ORACLE_CURSOR, (i + 1) * Constants.ORACLE_CURSOR)
//      val str = s" id in ('${newIdList.mkString("','")}')"
////      println(str)
//      IOutil.writeFile(fileName,str)
//    }
//    println(s"龙岗数据库中缺的数据已存入到文件中,保存的数量为: ${idList.length} 条")
//  }
//  def saveIdToFile(fileName: String,idList: List[String])={
//
//    println(s"龙岗数据库中缺的数据已存入到文件中,保存的数量为: ${idList.length} 条")
//  }

//  def main(args: Array[String]): Unit = {
//    saveIdToFile("aa",List("1","2","3","4","5"))
//  }

  def saveFieldToField(jobName: String): Unit ={
    var tableName=""
    jobName match {
      case "building" => tableName=Constants.TMP_LG_BUILDING
      case "house" => tableName=Constants.TMP_LG_HOUSE
      case "resident" => tableName=Constants.TMP_LG_RESIDENT
      case "residenthmt" => tableName=Constants.TMP_LG_RESIDENTHMT
      case "residentoutside" => tableName=Constants.TMP_LG_RESIDENTOUTSIDE
      case "residentlogout" => tableName=Constants.TMP_LG_RESIDENTLOGOUT
      case "enterprise" => tableName=Constants.TMP_LG_ENTERPRISEINFO
      case "qharea" => tableName=Constants.TMP_LG_QHAREA
      case "qhteam" => tableName=Constants.TMP_LG_QHTEAM
      case "event" => tableName=Constants.TMP_LG_EVENT
      case "enterpriseinfomap" => tableName=Constants.TMP_LG_ENTERPRISEINFOMAP
    }
    val strings = JDBCUtil.getFieldFromTableName(tableName)
    IOutil.writeFile(jobName, strings.mkString("\",\""))
  }
}
